package homework.billing.ui;

import homework.billing.meter.FullMeter;
import homework.billing.timer.LabelClock;
import homework.billing.timer.Listener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MeterWindow extends JFrame {

    private JComboBox comboBox;

    private JLabel mileageLabel;

    private JLabel speedLabel;

    private JLabel priceLabel;

    private JLabel timeLabel;

    private float mileage = 0;

    private float waitingTime = 0;

    private LabelClock clock;

    private JScrollBar bar;

    class ReceiveSeconds implements Listener {

        private JLabel timeLabel;
        private LabelClock clock;

        public ReceiveSeconds(JLabel tl) {
            timeLabel = tl;
        }

        @Override
        public void receiveTime(long seconds) {
            timeLabel.setText(clock.formatTime());
            float speed = bar.getValue();
            if(speed == 0) waitingTime++;

            float km = speed/100000.f;
            setSpeed(km);

            setMileage(getMileage()+km);
            double money = getCurrentMeter().cost(getMileage(), 0, seconds/60.f);
            setMoney(money);
        }

        public void listen(LabelClock clock) {
            clock.addListener(this);
            this.clock = clock;
        }
    }

    public MeterWindow() {
        super();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("计费器");

        JPanel panel = new JPanel();
        setContentPane(panel);
        panel.setLayout(new FlowLayout());
        panel.setPreferredSize(new Dimension(240, 160));

        // 显示计速
        Box hbox = Box.createHorizontalBox();
        speedLabel = new JLabel();
        setSpeed(0);

        // 里程
        mileageLabel = new JLabel();
        setMileage(0);

        hbox.add(speedLabel);
        hbox.add(Box.createHorizontalStrut(10));
        hbox.add(mileageLabel);

        Box vbox = Box.createVerticalBox();
        vbox.add(hbox);

        comboBox = new JComboBox();
        vbox.add(comboBox);

        // 速度滚动条
        bar = new JScrollBar(JScrollBar.HORIZONTAL);
        bar.setMaximum(1666);
        vbox.add(bar);

        hbox = Box.createHorizontalBox();
        timeLabel = new JLabel("00:00:00");
        clock = new LabelClock();
        hbox.add(timeLabel);
        hbox.add(Box.createHorizontalStrut(10));

        priceLabel = new JLabel("0 元");
        hbox.add(priceLabel);
        vbox.add(hbox);

        new ReceiveSeconds(timeLabel).listen(clock);

        JButton btn = new JButton("Start");


        JButton resetBtn = new JButton("Reset");
        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                reset();
            }
        });

//        resetBtn.addActionListener((e) -> reset());

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if(clock.isRunning()) {
                    clock.stop();
                    resetBtn.setEnabled(true);
                    comboBox.setEnabled(true);
                    btn.setText("Start");
                } else {
                    reset();
                    SelectOption so = (SelectOption) comboBox.getSelectedItem();
                    clock.start();
                    resetBtn.setEnabled(false);
                    comboBox.setEnabled(false);
                    btn.setText("Stop");
                }
            }
        });

//        btn.addActionListener((e) -> {
//            if(clock.isRunning()) {
//                clock.stop();
//                resetBtn.setEnabled(true);
//                comboBox.setEnabled(true);
//                btn.setText("Start");
//            } else {
//                reset();
//                SelectOption so = (SelectOption) comboBox.getSelectedItem();
//                clock.start();
//                resetBtn.setEnabled(false);
//                comboBox.setEnabled(false);
//                btn.setText("Stop");
//            }
//        });

        hbox = Box.createHorizontalBox();
        hbox.add(btn);
        hbox.add(resetBtn);
        vbox.add(hbox);

        panel.add(vbox,BorderLayout.CENTER);
    }

    public void addOption(SelectOption option) {
        comboBox.addItem(option);
    }

    public FullMeter getCurrentMeter() {
        SelectOption opt = (SelectOption)comboBox.getSelectedItem();

        return opt.getMeter();
    }

    public void setSpeed(float speed) {
        speedLabel.setText(String.format("%.2f", speed*3600)+" km/h");
    }

    public void setMileage(float mileage) {
        this.mileage = mileage;
        mileageLabel.setText(String.format("%.1f", mileage) +" km");
    }

    public float getMileage() {
        return mileage;
    }

    public void setMoney(double money) {
        priceLabel.setText(String.format("%.2f", money));
    }

    public void reset() {
        bar.setValue(0);
        setSpeed(0);
        setMoney(0);
        setMileage(0);
        clock.resetSecond();
        timeLabel.setText("00:00:00");
    }
}
